/* ====================================================================
   Copyright (C) 2004-2008  fyiReporting Software, LLC
    Copyright (C) 2010 Krzysztof Marecki

    This file is part of the NReports project.
Copyright (C) 2010 Krzysztof Marecki
   This file is part of the NReports project.
Copyright (C) 2010 Krzysztof Marecki
   This file is part of the NReports project.
   Copyright (C) 2010  Krzysztof Marecki

   This file is part of the GtkReporting project.
	
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Text;

using NReports.RDL;

namespace NReports.RDL
{
    [Serializable]
    internal class FunctionFormatNumber : IExpr
    {
        IExpr _Formatee;	// object to format
		IExpr _Digits;		// format string
		/// <summary>
		/// Format an object as a number
		/// </summary>
		public FunctionFormatNumber(IExpr formatee, IExpr digits) 
		{
			_Formatee = formatee;
			_Digits= digits;
		}

        #region IExpr Members

        public TypeCode GetTypeCode()
        {
            return TypeCode.String;
        }

        public bool IsConstant()
        {
            if (_Formatee.IsConstant())
                return _Digits.IsConstant();

            return false;
        }

        public IExpr ConstantOptimization()
        {

            _Formatee = _Formatee.ConstantOptimization();
            _Digits = _Digits.ConstantOptimization();
            if (_Formatee.IsConstant() && _Digits.IsConstant())
            {
                string s = EvaluateString(null, null);
                return new ConstantString(s);
            }

            return this;
        }

        public object Evaluate(Report r, Row row)
        {
            return EvaluateString(r, row);
        }

        public string EvaluateString(Report r, Row row)
        {
            object o = _Formatee.Evaluate(r, row);
            if (o == null)
                return null;

            int digits = _Digits.EvaluateInt32(r, row);
            string format = string.Format(@"{{0:N{0}}}", digits);
            string result = string.Format(format, o);

            return result;
        }

        public double EvaluateDouble(Report r, Row row)
        {
            string result = EvaluateString(r, row);
            return Convert.ToDouble(result);
        }

        public decimal EvaluateDecimal(Report r, Row row)
        {
            string result = EvaluateString(r, row);
            return Convert.ToDecimal(result);
        }

        public int EvaluateInt32(Report r, Row row)
        {
            string result = EvaluateString(r, row);
            return Convert.ToInt32(result);
        }

        public DateTime EvaluateDateTime(Report r, Row row)
        {
            string result = EvaluateString(r, row);
            return Convert.ToDateTime(result);
        }

        public bool EvaluateBoolean(Report r, Row row)
        {
            string result = EvaluateString(r, row);

            return Convert.ToBoolean(result);
        }

        #endregion
    }
}
